﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.DataEntity;
using Cosmetic.Logic;

namespace WebApp.Management.Product
{
    public partial class HotKeyWordManager : PageBase2
    {
        ProductManager pm = new ProductManager();
        private RoleManager m_RoleManager = new RoleManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser1 == null)
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
            if (!IsPostBack)
            {
                Cosmetic.DataEntity.User theUser = this.SignInUser1;
                if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
                {
                    Session.Remove("SignInUser1");
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                    return;
                }
            }
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            this.lvHotKeyWord.DataSource = pm.GetHotKeyWord();
            this.lvHotKeyWord.DataBind();
        }

        protected void lvSupCategory_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("OnNew"))
            {
                this.lvHotKeyWord.InsertItemPosition = InsertItemPosition.LastItem;
            }
            else if (e.CommandName.Equals("OnInsert"))
            {
                //if (m_CategoryManager.IsExistTheName((e.Item.FindControl("txtName") as TextBox).Text.Trim()))
                //{
                //    Cosmetic.WebGeneric.WebMessageBox("该类下包含有此类！", this.Page);
                //    return;
                //}
                try
                {
                    int.Parse((e.Item.FindControl("txtViewTimes") as TextBox).Text.Trim());
                }
                catch (Exception)
                {
                    Cosmetic.WebGeneric.WebMessageBox("序列必须是整数！", this.Page);
                    return;
                }
                string keyword = (e.Item.FindControl("txtName") as TextBox).Text.Trim();
                int sequence = int.Parse((e.Item.FindControl("txtViewTimes") as TextBox).Text.Trim());

                pm.InsertKeyWord(keyword, sequence);

                this.lvHotKeyWord.InsertItemPosition = InsertItemPosition.None;
            }
            else if (e.CommandName.Equals("OnEdit"))
            {
                this.lvHotKeyWord.EditIndex = this.lvHotKeyWord.Items.IndexOf(e.Item as ListViewDataItem);
            }
            else if (e.CommandName.Equals("OnUpdate"))
            {
                try
                {
                    int.Parse((e.Item.FindControl("txtViewTimes") as TextBox).Text.Trim());
                }
                catch (Exception)
                {
                    Cosmetic.WebGeneric.WebMessageBox("序列必须是整数！", this.Page);
                    return;
                }
                HotKeyWord theHotKeyWord = pm.GetHotKeyWordById(int.Parse(e.CommandArgument.ToString()));
                theHotKeyWord.HotKeyWord1 = (e.Item.FindControl("txtName") as TextBox).Text.Trim();
                theHotKeyWord.ViewTimes = int.Parse((e.Item.FindControl("txtViewTimes") as TextBox).Text.Trim());
                pm.Update();
                this.lvHotKeyWord.EditIndex = -1;
            }
            else if (e.CommandName.Equals("OnDelete"))
            {
                //HiddenField hfid = e.Item.FindControl("hfId") as HiddenField;

                //HotKeyWord theHotKeyWord = pm.GetHotKeyWordById(int.Parse(e.CommandArgument.ToString()));

                //Category theCategory = this.m_CategoryManager.GetCategoryById(new Guid(e.CommandArgument.ToString()));

                pm.DeleteKeyWord(int.Parse(e.CommandArgument.ToString()));

                //if (this.m_CategoryManager.GetSubCateogoriesBySupCategory(theCategory).Count > 0)
                //{
                //    Cosmetic.WebGeneric.WebMessageBox("该分类下包含有子分类，请先删除子分类!", this.Page);
                //    return;
                //}
                //else
                //{
                //    this.m_CategoryManager.Delete(theCategory);
                //}
            }
            else if (e.CommandName.Equals("OnCancel"))
            {
                this.lvHotKeyWord.EditIndex = -1;
                this.lvHotKeyWord.InsertItemPosition = InsertItemPosition.None;
            }
            BindData();
        }
    }
}